<template>
	<default-header-page-layout ref="page" title="拆垛入库" >
		<view class="page-frame with-action-user-row" :style="{height:pageBodyHeight+'px'}" v-if="pageBodyHeight">
			<action-user-row />
			<view class="with-action-user-row-page-content">
				<page-main v-show="pageStyle==='main'" ref="main" @pageChange="onMainPageChange" />
				<page-edit v-show="pageStyle==='edit'" ref="edit" />
				<page-step2 v-show="pageStyle==='step2'" ref="step2" />
			</view>
		</view>
		<template v-slot:footer>
			<view class="bottom-btns-row">
				<template v-if="pageStyle==='main'">
					<view class="btn-frame"><u-button text="组 盘" @click="onBind"></u-button></view>
					<view class="divider"></view>
					<view class="btn-frame"><u-button type="primary" text="下一步" @click="onGoStep2"></u-button></view>
				</template>
				<template v-else>
					<view class="btn-frame"><u-button text="返 回" @click="onBackMain"></u-button></view>
					<view class="divider"></view>
					<view class="btn-frame" v-if="pageStyle==='step2'"><u-button type="primary" text="入 库" @click="onConfirm"></u-button></view>
					<view class="btn-frame" v-else><u-button type="primary" :text="editIndex===-1?'添 加':'修 改'" @click="onEditConfirm"></u-button></view>
				</template>
			</view>
		</template>
	</default-header-page-layout>
</template>

<script>
import DefaultHeaderPageLayout from '@/components/DefaultHeaderPageLayout.vue'
import ActionUserRow from '@/components/ActionUserRow.vue'
import PageMain from './modules/main.vue'
import PageEdit from './modules/edit.vue'
import PageStep2 from './modules/step2.vue'
import { $successInfo } from '@/static/js/utils/index.js'
let initInterVal = null;
export default {
	name:'unstackingInstorePage',
	components:{DefaultHeaderPageLayout,ActionUserRow,PageMain,PageEdit,PageStep2},
	data(){
		return {
			pageBodyHeight:0,
			pageStyle:'main',
			editIndex:-1,
			mainData:{}
		}
	},
	methods:{
		onBackMain(){
			this.pageStyle = 'main'
		},
		onGoStep2(){
			let check = this.$refs.main.checkConfirm()
			if (check.flag) {
				this.mainData = check.data
				this.pageStyle = 'step2'
				this.$refs.step2.init()
			}
		},
		onEditConfirm(){
			let check = this.$refs.edit.checkConfirm()
			if (check.flag) {
				if (this.editIndex===-1) {
					this.$refs.main.add(check.data)
				} else {
					this.$refs.main.modify(check.data,this.editIndex)
				}
			}
			this.pageStyle = 'main'
		},
		onMainPageChange(obj,index){
			if (obj) {
				this.editIndex = index
			} else {
				this.editIndex = -1
			}
			this.$refs.edit.init(obj)
			this.pageStyle = 'edit'
		},
		onMainReset(){
			this.$refs.main.clearMain()
		},
		onBind(){
			let check = this.$refs.main.checkConfirm('bind')
			if (check.flag) {
				this.dealBind(check.data,(f)=>{
					if (f) {
						$successInfo('组盘成功')
					}
				})
			}
		},
		onConfirm(){
			this.dealConfirm(this.mainData,(f)=>{
				if (f) {
					$successInfo('提交成功')
					this.mainData = {}
					this.$refs.main.clearMain()
					this.$refs.step2.clear()
				}
			})
		},
		/* 组盘接口调用 */
		dealBind(obj,callback){
			let params = this.dealSubmitParams(obj,'bind')
			this.$api.post('UnstackingBindEntrance',params,{block:'unstacking'}).then(()=>{
				callback && callback(true)
			}).catch((e)=>{
				callback && callback(false,e)
			})
		},
		/* 入库接口调用 */
		dealConfirm(obj,callback){
			let params = this.dealSubmitParams(obj)
			this.$api.post('AccessoriesAutoWare',params,{block:'unstacking'}).then(()=>{
				callback && callback(true)
			}).catch((e)=>{
				callback && callback(false,e)
			})
		},
		/* 组盘、入库提交参数 */
		dealSubmitParams(obj,actionType='confirm'){
			let res = {
				containerCode:obj.container.containerCode,
				wmsMaterials:obj.list
			}
			if (actionType==='confirm') {
				let params2 = this.$refs.step2.get()
				res = {...res,...params2}
			}
			return res
		},
		/* 页面初始化获取页面body高度的定时器 */
		startInitInterval(callback){
			initInterVal = setInterval(()=>{
				if (this.pageBodyHeight) {
					this.clearInitInterval()
					callback && callback()
				} else {
					this.pageBodyHeight = this.$refs.page.getBodyHeight()
				}
			},200)
		},
		/* 清除定时器 */
		clearInitInterval(){
			try{
				clearInterval(initInterVal)
				initInterVal = null
			}catch(e){
				//TODO handle the exception
			}
		}
	},
	onReady(){
		this.startInitInterval(()=>{
			/* 页面初始化后需要执行的代码在这边调用 */
		})
	},
	onUnload(){
		this.clearInitInterval()
	}
}
</script>

<style scoped lang="scss">
.bottom-btns-row{
	display: flex;
	padding: 10rpx 20rpx;
	background-color: #fff;
	.btn-frame{
		width: 1%;
		box-sizing: border-box;
		flex-grow: 1;
	}
	.divider{
		width: 20rpx;
		flex-shrink: 0;
	}
}
</style>